package cn.zh.stan.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.SItem;
import cn.zh.stan.R;
import cn.zh.stan.adapter.SFAdapter;
import cn.zh.stan.util.MyListView;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class SendFormat extends BaseActivity {

    ImageView sd_back;

    MyListView sflist;

    SFAdapter sfAdapter;
    ArrayList<SItem> sItems=new ArrayList<SItem>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_format);
        inisd();
        gettrade();
       }


    public void inisd(){
        sd_back= (ImageView) findViewById(R.id.sd_back);
        sflist= (MyListView) findViewById(R.id.sflist);
        sfAdapter=new SFAdapter(SendFormat.this,sItems);
        sflist.setAdapter(sfAdapter);
        sd_back.setOnClickListener(this);
        sflist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (int j=0;j<sItems.size();j++){
                    if (j==i){
                        sItems.get(j).setType("1");
                    }else {
                        sItems.get(j).setType("0");
                    }
                  }
                sfAdapter.notifyDataSetChanged();
                Intent intent=new Intent(SendFormat.this,FilOrder.class);
                intent.putExtra("oj",sItems.get(i));
                setResult(1,intent);
                finish();
            }
        });
      }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.sd_back:
                finish();
                break;
        }
    }




    public void gettrade(){
        OkHttpUtils.get(BaseUrl.trade_get)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("--------->获取配送方式===="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                boolean Flag=js.getBoolean("Flag");
                                if (Flag){
                                    JSONArray ja=js.getJSONArray("Res");
                                    sItems.clear();
                                    for (int i=0;i<ja.length();i++){
                                     JSONObject ji=ja.getJSONObject(i);
                                        String Name=ji.getString("Name");
                                        String LinkMan=ji.getString("LinkMan");
                                        String Tel=ji.getString("Tel");
                                        String bMonthCharge=ji.getString("bMonthCharge");
                                        String BillStyle=ji.getString("BillStyle");
                                        String Remark=ji.getString("Remark");
                                        String OrderPos=ji.getString("OrderPos");
                                        String LogisticID=ji.getString("LogisticID");
                                        String WebSite=ji.getString("WebSite");
                                        String bBlockUp=ji.getString("bBlockUp");
                                        String Balance=ji.getString("Balance");
                                        String bSendStyle=ji.getString("bSendStyle");
                                        String type="0";
                                    SItem sItem=new SItem(Name,LinkMan,Tel,bMonthCharge,BillStyle,Remark,OrderPos,LogisticID,WebSite,bBlockUp,Balance,bSendStyle,type);
                                       sItems.add(sItem);
                                    }
                                    sfAdapter.notifyDataSetChanged();
                                }else {
                                String Msg=js.getString("Msg");
                                  ToastUtil.showLong(SendFormat.this,Msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
              }

}
